﻿using System;
using System.IO;
using TeachMe.Application;

namespace TeachMe.DictionaryConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1 || !File.Exists(args[0]))
            {
                throw new ArgumentException();
            }

            XmlEditor source = XmlEditor.Load(args[0]);

            if (!source.HasAttributeValue("version"))
            {
                ConvertZeroToOne(source);
            }
        }

        private static void ConvertZeroToOne(XmlEditor source)
        {
            string directory = Path.GetDirectoryName(source.Path);

            XmlEditor destination = XmlEditor.Create(Utilities.GetRandomFileName(directory, ".xml"), "Dictionary");

            Console.WriteLine(destination.Path);

            // header

            string id = source.HasAttributeValue("id") ? source.GetAttributeValue("id") : Guid.NewGuid().ToString();
            string name = source.GetAttributeValue("name");
            string description = source.GetAttributeValue("description");
            string fromLanguage = source.GetAttributeValue("from");
            string toLanguage = source.GetAttributeValue("to");

            destination.SetAttributeValue("id", id);
            destination.SetAttributeValue("name", name);
            destination.SetAttributeValue("description", description);
            destination.SetAttributeValue("from", fromLanguage);
            destination.SetAttributeValue("to", toLanguage);
            destination.SetAttributeValue("id", id);
            destination.SetAttributeValue("version", "1");

            // entries

            int counter = 0;

            using (source.LoadNode("Entries"))
            {
                using (destination.AddNode("Entries"))
                {
                    foreach (IDisposable entryNode in source.LoadChildNodes())
                    {
                        string from = null, to = null, example = null;

                        using (entryNode)
                        {
                            if (source.HasChildNode("From"))
                            {
                                using (source.LoadNode("From"))
                                {
                                    from = source.GetTextValue();
                                }
                            }

                            if (source.HasChildNode("To"))
                            {
                                using (source.LoadNode("To"))
                                {
                                    to = source.GetTextValue();
                                }
                            }

                            if (source.HasChildNode("Example"))
                            {
                                using (source.LoadNode("Example"))
                                {
                                    example = source.GetTextValue();
                                }
                            }
                        }

                        using (destination.AddNode("Entry"))
                        {
                            destination.SetAttributeValue("id", (counter++).ToString());

                            if (!String.IsNullOrEmpty(from))
                            {
                                using (destination.AddNode("From"))
                                {
                                    destination.SetTextValue(from);
                                }
                            }
                            if (!String.IsNullOrEmpty(to))
                            {
                                using (destination.AddNode("To"))
                                {
                                    destination.SetTextValue(to);
                                }
                            }
                            if (!String.IsNullOrEmpty(example))
                            {
                                using (destination.AddNode("Example"))
                                {
                                    destination.SetTextValue(example);
                                }
                            }
                        }
                    }
                }
            }

            destination.Save();
        }
    }
}
